<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
  <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
  <link rel="stylesheet" href="__CSS__/admin.css"  media="all">
  <!--<script src="http://{$Think.config.google_map_url}" async="async"></script>-->
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
  <style>
    .layui-upload-img{
      width: 100px;
    }
    .layui-table th{
      padding: 9px 6px;
    }
  </style>
</head>
<body>
<div style="margin-top: 20px;">
</div>

<form class="layui-form" id="admin">


  <div class="layui-form-item " >
    <label class="layui-form-label">选择会员</label>
    <div class="layui-input-inline" style="position: relative;z-index: 10000;width: 400px">
      <select  id="trip"  lay-filter="addtrips"   lay-verify="required" lay-search>
        {volist name="members" id="vo"}
        <option value="{$vo.member_id}">{$vo.member_nickname}</option>
        {/volist}
      </select>
    </div>
    <span id="addtrips" data-name="" data-id="0" class="layui-btn layui-btn-danger layui-btn-sm" style="margin-top: 4px">添加</span>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">已选会员</label>
    <div class="layui-input-inline" style="width: 500px" id="tripnames">
    </div>
  </div>
  <input name="coupon_userange" id="tripids"  value="0"   type="hidden">
  <input name="id"   value="{$coupon_id}"   type="hidden">

  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="admin">派送</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>

</form>


<script src="__PUBLIC__/layui/layui.js"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>

<script>
  layui.use(['layer', 'form','laydate'], function() {
      // var laydate = layui.laydate;
    var layer = layui.layer,
            $ = layui.jquery,
        laydate= layui.laydate,
            form = layui.form;

    $(window).on('load', function() {

        //商品范围选择下拉添加事件
        form.on('select(addtrips)', function(data){
            var tripid=data.value;
            $("#addtrips").attr("data-id",tripid);
        });

        $("#addtrips").on("click",function () {
            var tripid= $("#addtrips").attr("data-id");
            var tripids=$("#tripids").val();
            var ids=tripids.split(",");

            if (tripid==0){
                tripids=999999;
                var tripspan='';
                $("#tripnames").html(tripspan);
            }else{

                for(var i=0 ; i < ids.length; i++){
                    if(tripid==ids[i]){
                        layer.msg("不能添加重复人员");
                        return false;
                    }
                }
                var tripname="";
                $("#trip").find("option").each(function () {
                    if($(this).val()==tripid){
                        tripname=$(this).html();
                    }
                });

                var tripspan='<span class="layui-btn layui-btn-sm layui-btn-normal deltripspan" data-id="'+tripid+'" style="margin: 5px 10px">'+tripname+'<i class="layui-icon"></i></span>';

                if(!tripids){
                    tripids+=tripid;
                }else{
                    tripids+=','+tripid;
                }
                $("#tripnames").append(tripspan);
            }



            $("#tripids").val(tripids);
        })


        $(document).on("click",'.deltripspan',function () {
            var dataid=$(this).attr("data-id");
            var tripids=$("#tripids").val();
            var ids=tripids.split(",");
            var newid="";
            for(var i=0 ; i < ids.length; i++){
                if(ids[i]&&dataid!=ids[i]){
                    if(!newid){
                        newid+=ids[i];
                    }else{
                        newid+=","+ids[i];
                    }
                }
            }
            $("#tripids").val(newid);
            $(this).remove();
        })



      form.on('submit(admin)', function(data) {

        $.ajax({
          url:"{:url('admin/Coupon/distributed')}",
          data:$('#admin').serialize(),
          type:'post',
          async: false,
          success:function(res) {
            layer.msg(res.msg);
            if(res.code == 1) {
              setTimeout(function(){
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
              },1500)
            }
          }
        })
        return false;
      });
    });
  });
</script>
<!-- 配置文件 -->
<script type="text/javascript" src="__PUBLIC__/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="__PUBLIC__/ueditor/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
</body>
</html>
